package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CoursePublicVo;
import com.atguigu.eduservice.entity.vo.courseInfoVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(courseInfoVo courseInfoVo) {

        EduCourse educourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,educourse);
        int insert = baseMapper.insert(educourse);
        if(insert<=0){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("添加课程失败");
            }
        }
        String cid=educourse.getId();
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        courseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    @Override
    public courseInfoVo getCourseInfo(String courseId) {

        EduCourse eduCourse = baseMapper.selectById(courseId);

        courseInfoVo courseInfoVo=new courseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
         courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(courseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
      if(i==0){
       System.out.println("修改课程失败");
   }
        EduCourseDescription description=new EduCourseDescription();
                description.setId(courseInfoVo.getId());
                description.setDescription(courseInfoVo.getDescription());
          courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublicVo publicCourseInfo(String id) {
        CoursePublicVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String id) {
       eduVideoService.removeVideoByCourse(id);
       eduChapterService.removeChapterByCourse(id);
       courseDescriptionService.removeById(id);
        int i = baseMapper.deleteById(id);
        if(i==0){
            throw new GuliException(20001,"删除失败");
        }
    }
}
